<template>
  <el-dialog
    :title="!dataForm.salaryId? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="员工" prop="userId">
            <el-select v-model="dataForm.userId" placeholder="请选择员工">
              <el-option
                v-for="item in ygOption"
                :key="item.value"
                :label="item.label"
                :value="item.value">
                <span style="float: left">{{ item.label }}</span>
                <span style="float: right; color: #8492a6; font-size: 13px">{{ item.value }}</span>
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item prop="salaryTime">
            <span style="display: flex; align-items: center;">
              <span style="flex-shrink: 0;">工资发放时间：</span>
              <el-date-picker
                v-model="dataForm.salaryTime"
                type="date"
                placeholder="选择日期">
              </el-date-picker>
            </span>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="应发工资" prop="salaryNum">
            <el-input v-model="dataForm.salaryNum" placeholder="应发工资"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="扣除工资" prop="salarySub">
            <el-input v-model="dataForm.salarySub" placeholder="扣除工资"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <el-form-item label="五险一金" prop="salaryWxyz">
            <el-input v-model="dataForm.salaryWxyz" placeholder="五险一金"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="实际工资" prop="salaryFact">
            <el-input v-model="dataForm.salaryFact" placeholder="实际工资" disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="kpi" prop="kpi">
            <el-input v-model="dataForm.kpi" placeholder="kpi"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="备注" prop="note">
        <el-input v-model="dataForm.note" placeholder="备注" style="overflow: auto"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      ygOption: [],
      visible: false,
      dataForm: {
        salaryId: 0,
        userId: '',
        salaryTime: '',
        salaryNum: '',
        note: '',
        salaryWxyz: '',
        salaryFact: '',
        salarySub: '',
        kpi: ''
      },
      dataRule: {
        userId: [
          { required: true, message: '员工id不能为空', trigger: 'blur' }
        ],
        salaryTime: [
          { required: true, message: '工资发放时间不能为空', trigger: 'blur' }
        ],
        salaryNum: [
          { required: true, message: '应发工资不能为空', trigger: 'blur' }
        ],
        note: [
          { required: true, message: '备注不能为空', trigger: 'blur' }
        ],
        salaryWxyz: [
          { required: true, message: '五险一金（500）不能为空', trigger: 'blur' }
        ],
        salarySub: [
          { required: true, message: '扣除工资不能为空', trigger: 'blur' }
        ]
      }
    };
  },
  watch: {
    'dataForm.salaryNum'(newVal) {
      this.dataForm.salaryFact = this.actualSalaryFact;
    },
    'dataForm.salarySub'(newVal) {
      this.dataForm.salaryFact = this.actualSalaryFact;
    },
    'dataForm.salaryWxyz'(newVal) {
      this.dataForm.salaryFact = this.actualSalaryFact;
    },
    'dataForm.kpi'(newVal) {
      this.dataForm.salaryFact = this.actualSalaryFact;
    }
  },
  computed: {
    // 计算实际工资的计算属性
    actualSalaryFact() {
      const num = parseFloat(this.dataForm.salaryNum) || 0;
      const sub = parseFloat(this.dataForm.salarySub) || 0;
      const wxyj = parseFloat(this.dataForm.salaryWxyz) || 0;
      const kpi = parseFloat(this.dataForm.kpi) || 0;
      // 先计算
      let result = num - sub - wxyj + kpi;

      // 使用Math.round()进行四舍五入并转换为int
      // 直接更新 dataForm.salaryFact
      this.dataForm.salaryFact = Math.round(result);
      return this.dataForm.salaryFact;
    }
  },
  methods: {
    init(id) {
      this.dataForm.salaryId = id || 0;
      this.visible = true;
      this.getDataList();
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields();
        if (this.dataForm.salaryId) {
          this.$http({
            url: this.$http.adornUrl(`/ask/salary/info/${this.dataForm.salaryId}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.userId = data.salary.userId;
              this.dataForm.salaryTime = data.salary.salaryTime;
              this.dataForm.salaryNum = data.salary.salaryNum;
              this.dataForm.note = data.salary.note;
              this.dataForm.salaryWxyz = data.salary.salaryWxyz;
              this.dataForm.salaryFact = data.salary.salaryFact;
              this.dataForm.salarySub = data.salary.salarySub;
              this.dataForm.kpi = data.salary.kpi;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/ask/salary/${!this.dataForm.salaryId? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'salaryId': this.dataForm.salaryId || undefined,
              'userId': this.dataForm.userId,
              'salaryTime': this.dataForm.salaryTime,
              'salaryNum': this.dataForm.salaryNum,
              'note': this.dataForm.note,
              'salaryWxyz': this.dataForm.salaryWxyz,
              'salaryFact': this.dataForm.salaryFact,
              'salarySub': this.dataForm.salarySub,
              'kpi': this.dataForm.kpi
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit('refreshDataList');
                }
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
    getDataList() {
      this.$http({
        url: this.$http.adornUrl('/sys/user/list'),
        method: 'get',
        params: this.$http.adornParams({})
      }).then(({ data }) => {
        this.ygOption = data.page.list.map((item) => {
          return {
            label: item.realName,
            value: item.userId
          };
        });
      });
    }
  }
};
</script>
